package com.ruicar.afs.cloud.afscase.channel.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.channel.common.Constants;
import com.ruicar.afs.cloud.afscase.channel.condition.CarDealerCondition;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelCoopeCardealer;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelMainBrand;
import com.ruicar.afs.cloud.afscase.channel.entity.CommonCarDealer;
import com.ruicar.afs.cloud.afscase.channel.mapper.CarDealerMapper;
import com.ruicar.afs.cloud.afscase.channel.service.CarDealerService;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelCoopeCardealerService;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelMainBrandService;
import com.ruicar.afs.cloud.afscase.channel.vo.AllCarDealerVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: sang jian
 * @date: 2020/5/14 10:55
 * @description:
 */
@Service
@AllArgsConstructor
public class CarDealerServiceImpl extends ServiceImpl<CarDealerMapper, CommonCarDealer> implements CarDealerService {

    private ChannelMainBrandService channelMainBrandService;
    private ChannelCoopeCardealerService channelCoopeCardealerService;

    @Override
    /**根据合作商账户返回此账户下的车商列表信息*/
    public IResponse getCarDealersByUserId(String userId, String cardealerType, String aSwitch, String channelId, String businessType) {
        if (Constants.COMMONCARDEALER.equals(cardealerType) && Constants.PERSON_SWITCH_ON.equals(aSwitch)) {
            return IResponse.success(baseMapper.getCommonCarDealersByUserId(userId, cardealerType));
        } else if (Constants.DIRECTCARDEALER.equals(cardealerType)) {
            return IResponse.success(baseMapper.getDirectCarDealersByUserId(userId, cardealerType, businessType));
        } else if (Constants.COMMONCARDEALER.equals(cardealerType) && Constants.PERSON_SWITCH_OFF.equals(aSwitch)) {
            return IResponse.success(baseMapper.getCommonCarDealersByChannelId(channelId, cardealerType));
        }
        return IResponse.fail("传入的参数有误或渠道人员关联账号值为空");
    }


    @Override
    public IPage<AllCarDealerVo> getAllCarDealers(CarDealerCondition condition) {

        int pageNumber = condition.getPageNumber();
        int pageSize = condition.getPageSize();
        Page page = new Page(pageNumber,pageSize);
        return baseMapper.getAllCarDealers(page,condition);
    }

    @Override
    @Transactional
    public boolean saveCommonCarDealer(CarDealerCondition condition) {

        CommonCarDealer commonCarDealer = condition.getCommonCarDealer();
        List<ChannelCoopeCardealer> channelCoopeCardealerTemps = condition.getChannelCoopeCardealerTemps();
        String[] mainBrand = condition.getMainBrand();

        boolean result2 = saveOrUpdate(commonCarDealer);
        channelCoopeCardealerService.remove(Wrappers.<ChannelCoopeCardealer>query().lambda()
                .eq(ChannelCoopeCardealer::getCardealerId, commonCarDealer.getId())
                .eq(ChannelCoopeCardealer::getCardealerType, Constants.COMMONCARDEALER)
        );

        if(CollectionUtil.isNotEmpty(channelCoopeCardealerTemps)){
            for (ChannelCoopeCardealer cardealerTemps : channelCoopeCardealerTemps){
                cardealerTemps.setId(null);
            }
        }
        boolean result3 = channelCoopeCardealerService.saveOrUpdateBatch(channelCoopeCardealerTemps);

        //更新车商主营品牌
        List<ChannelMainBrand> mainBrandList = channelMainBrandService.list(Wrappers.<ChannelMainBrand>query().lambda()
                .eq(ChannelMainBrand::getChannelId, commonCarDealer.getId())
                .eq(ChannelMainBrand::getChannelBelong, Constants.COMMONCARDEALER)
        );
        if (mainBrandList != null && mainBrandList.size() > 0) {
            for (ChannelMainBrand brand : mainBrandList) {
                channelMainBrandService.removeById(brand.getId());
            }
        }
        List<ChannelMainBrand> channelMainBrandList = new ArrayList<>();
        if (mainBrand != null && mainBrand.length > 0) {
            for (String main : mainBrand) {
                ChannelMainBrand channelMainBrand = new ChannelMainBrand();
                String code = main.split(",")[0];
                String title = main.split(",")[1];
                channelMainBrand.setChannelId(commonCarDealer.getId());
                channelMainBrand.setCode(code);
                channelMainBrand.setTitle(title);
                channelMainBrand.setChannelBelong(Constants.COMMONCARDEALER);
                channelMainBrandList.add(channelMainBrand);
            }
            channelMainBrandService.saveOrUpdateBatch(channelMainBrandList);
        }
        return result2 && result3 ;
    }

    @Override
    public boolean changeStatus(CarDealerCondition condition) {
        String[] ids = condition.getIds();
        String channelId = condition.getChannelId();
        String status = condition.getStatus();
        if (ids == null || channelId == null) {
            return false;
        }
        boolean update = true;
        for (int i = 0; i < ids.length; i++) {
            //更新中间表
            List<ChannelCoopeCardealer> coopeCardealers = channelCoopeCardealerService.list(Wrappers.<ChannelCoopeCardealer>query().lambda()
                    .eq(ChannelCoopeCardealer::getCardealerId, ids[i])
                    .eq(ChannelCoopeCardealer::getChannelId, channelId)
                    .eq(ChannelCoopeCardealer::getCardealerType, Constants.COMMONCARDEALER));
            if (coopeCardealers != null && coopeCardealers.size() > 0) {
                if (coopeCardealers != null && coopeCardealers.size() > 0) {
                    for (ChannelCoopeCardealer coopeCardealer : coopeCardealers) {
                        coopeCardealer.setStatus(status);
                    }
                }
                update = channelCoopeCardealerService.updateBatchById(coopeCardealers);
            }
        }
        return update;
    }
    @Override
    /*根据合作商账户返回此账户下的车商列表信息(userids集合)*/
    public IResponse getCarDealersByUserIds(List<String> userIds, String cardealerType, String aSwitch, String channelId, String businessType) {

        if (Constants.COMMONCARDEALER.equals(cardealerType) && Constants.PERSON_SWITCH_ON.equals(aSwitch)) {
            return IResponse.success(baseMapper.getCommonCarDealersByUserIds(userIds, cardealerType));
        } else if (Constants.DIRECTCARDEALER.equals(cardealerType)) {
            return IResponse.success(baseMapper.getDirectCarDealersByUserIds(userIds, cardealerType, businessType));
        } else if (Constants.COMMONCARDEALER.equals(cardealerType) && Constants.PERSON_SWITCH_OFF.equals(aSwitch)) {
            return IResponse.success(baseMapper.getCommonCarDealersByChannelId(channelId, cardealerType));
        }
        return IResponse.fail("传入的参数有误或渠道人员关联账号值为空");
    }


}
